package com.holic.fetch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import com.holic.bean.Product;
import com.holic.util.Configuration;
import com.holic.util.LOG;


public class ListFetcher {
	
	private HttpClient client = new HttpClient();
	private List<Product> products = new ArrayList<Product>();
	private String category = ""; //当前抓取的种类
	private int currPage = 0; //当前抓取的页码
	private int totalPage = 2; //总页数,从页面中解析总页数
	
	public List<Product> fetch(String category){
		this.category = category;
		products.clear();
		while(currPage < totalPage){
			currPage++;
			GetMethod getMethod = null;
			try{
				getMethod = new GetMethod("http://btbtmall.net/shop/goods/goods_list.php?&category=" + category + "&page=" + currPage);
				client.executeMethod(getMethod);
				String result = getMethod.getResponseBodyAsString();
				LOG.info("第" + currPage + "页");
				parse(result);
				Thread.sleep(Configuration.getLong("fetch.interval", 0));
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				getMethod.releaseConnection();
			}
		}
		
//		try{
//			FileInputStream fis = new FileInputStream("f:/u.txt");
//			byte[] b = new byte[1024*1024*10];
//			int len = fis.read(b);
//			String s = new String(b, 0, len , "utf-8");
//			parse(s);
//		}catch (Exception e) {
//			e.printStackTrace();
//		}
		return products;
	}
	
	private void parse(String source){
		Matcher pageMatcher = Pattern.compile("category=(.*?)&page=(\\d+)").matcher(source); //获得总页数
		while(pageMatcher.find()){
			try{
				int num = Integer.parseInt(pageMatcher.group(2));
				if(num > totalPage){
					totalPage = num;
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		Matcher blockMatcher = Pattern.compile("class=\"goods\"([\\s\\S]*?)</div>").matcher(source);
		Pattern idPattern = Pattern.compile("goodsno=(.*?)&category");
		Pattern imgPattern = Pattern.compile("<img src='.*?/goods/(.*?)'");
		while(blockMatcher.find()){
			try{
				String block = blockMatcher.group(1);
				Matcher idMatcher = idPattern.matcher(block);
				Matcher imgMatcher = imgPattern.matcher(block);
				
				idMatcher.find();
				String id = idMatcher.group(1);
				String img = null;
				if(imgMatcher.find()){
				img = imgMatcher.group(1);
				}
				
				Product product = new Product();
				product.setId(Long.parseLong(id));
				if(null != img){
					System.out.println(img);
					product.setThumbnail(img);
					download(img);
				}
				LOG.info("抓取 => " + product.toString());
				products.add(product);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private void download(String filename){
		GetMethod getMethod = null;
		FileOutputStream fos = null;
		try{
			getMethod = new GetMethod("http://btbtmall.net/shop/data/goods/" + filename);
			client.executeMethod(getMethod);
			byte[] b = getMethod.getResponseBody();
			File file = new File(Configuration.getString("file.path") + File.separator + filename);
			if(!file.getParentFile().exists()){
				file.getParentFile().mkdirs();
			}
			fos = new FileOutputStream(file);
			fos.write(b);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				fos.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
			getMethod.releaseConnection();
		}
	}
	
	public void close(){
		client.getHttpConnectionManager().closeIdleConnections(0);
	}
	
}
